package com.marvels.financial.pay.cjpay.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.pay.McfeRepaymentTradeDetails;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.GenerateReqNoUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.ServerProperties;
import com.marvels.financial.pay.cjpay.dto.BindCardDTO;
import com.marvels.financial.pay.cjpay.dto.DirectPaymentDTO;
import com.marvels.financial.pay.cjpay.dto.DirectPaymentQueryDTO;
import com.marvels.financial.pay.cjpay.dto.MerchantDTO;
import com.marvels.financial.pay.cjpay.dto.PrebindCardDTO;
import com.marvels.financial.pay.cjpay.service.CJPayApi;
import com.marvels.financial.pay.cjpay.util.ChanPayUtil;
import com.marvels.financial.pay.cjpay.util.Post;
import com.marvels.financial.pay.cjpay.util.RSA;
import com.marvels.system.common.dao.McsmRevealConfigureAppDao;
import com.marvels.system.common.dto.McsmRevealConfigureAppDTO;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;

@Service
public class CJPayService implements CJPayApi {

	@Value("${config.properties.path}")
	private String configPropertiesPath;
	
	@Autowired
	private McsmRevealConfigureAppDao mcsmRevealConfigureAppDao;
	
	private final String charset = CharsetUtil.UTF_8;
	
	@Override
	public Map<String, Object> prebindCard(PrebindCardDTO bindCardDTO) throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>预绑卡请求参数:" + bindCardDTO);
		if(StringUtils.isEmpty(bindCardDTO.getBkAcctNo())){
			throw new MarvelsBusinessException("银行卡号不能为空");
		}
		if(StringUtils.isEmpty(bindCardDTO.getCstmrNm())){
			throw new MarvelsBusinessException("持卡人姓名不能为空");
		}
		if(StringUtils.isEmpty(bindCardDTO.getMobNo())){
			throw new MarvelsBusinessException("手机号不能为空");
		}
		if(StringUtils.isEmpty(bindCardDTO.getIdNo())){
			throw new MarvelsBusinessException("身份证不能为空");
		}
		if(StringUtils.isEmpty(bindCardDTO.getTrxId())){
			throw new MarvelsBusinessException("订单号不能为空");
		}

		// 商户号
		MerchantDTO merchant = getMerchantNoDK();
		
		// 组装业务参数
		Map<String, String> origMap = new HashMap<String, String>();
		origMap = setCommonMap(origMap, merchant.getMerchantNo());
		origMap.put("Service", "nmg_biz_api_auth_req");
		origMap.put("TrxId", bindCardDTO.getTrxId());
		origMap.put("ExpiredTime", bindCardDTO.getExpiredTime());
		origMap.put("MerUserId", bindCardDTO.getMerUserId());
		origMap.put("BkAcctTp", bindCardDTO.getBkAcctTp());
		origMap.put("BkAcctNo", RSA.encrypt(bindCardDTO.getBkAcctNo(), merchant.getPublicKey(), charset));
		origMap.put("IDTp", bindCardDTO.getIdTp());
		origMap.put("IDNo", RSA.encrypt(bindCardDTO.getIdNo(), merchant.getPublicKey(), charset));
		origMap.put("CstmrNm", RSA.encrypt(bindCardDTO.getCstmrNm(), merchant.getPublicKey(), charset));
		origMap.put("MobNo", RSA.encrypt(bindCardDTO.getMobNo(), merchant.getPublicKey(), charset));
		origMap.put("SmsFlag", bindCardDTO.getSmsFlag());
		
		String respStr = null;
		try {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷预绑卡请求报文:" + JSONObject.toJSONString(origMap));
			respStr = Post.gatewayPost(merchant.getRequestUrl(), origMap, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷预绑卡返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>orderNumber" + bindCardDTO.getTrxId() + ",idNumber" + bindCardDTO.getIdNo() + "预绑卡请求异常:", e1);
			throw new MarvelsBusinessException("预绑卡请求异常");
		}
		Map<String, Object> result=new HashMap<String, Object>();
		if(StrUtil.isBlank(respStr)) {
			throw new MarvelsBusinessException("返回结果为空");
	    }else{
	    	JSONObject resp = JSONObject.parseObject(respStr);
			if(StrUtil.equals(resp.getString("AcceptStatus"), "S") 
					&& StrUtil.equalsAny(resp.getString("Status"), "S", "P")){
				result.put("errormsg","成功");
				result.put("status","TO_VALIDATE");//沿用易宝的预绑卡订单状态  TO_VALIDATE  待短验
		    }else{
		    	result.put("errormsg", resp.get("RetMsg"));
				result.put("status","BIND_FAIL");
		    }
			result.put("errorcode", resp.get("RetCode"));
	    }
		return result;
	}

	@Override
	public Map<String, Object> bindCard(BindCardDTO bindCardDTO) throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>确定绑卡请求参数:" + JSON.toJSONString(bindCardDTO));
		//1 获取所需参数
		if(StringUtils.isEmpty(bindCardDTO.getSmsCode())){
			throw new MarvelsBusinessException("验证码不能为空");
		}
		if(StringUtils.isEmpty(bindCardDTO.getOriAuthTrxId())){
			throw new MarvelsBusinessException("订单号不能为空");
		}
		
		// 商户号
		MerchantDTO merchant = getMerchantNoDK();
		
		// 组装业务参数
		Map<String, String> origMap = new HashMap<String, String>();
		origMap = setCommonMap(origMap, merchant.getMerchantNo());
		origMap.put("Service", "nmg_api_auth_sms");
		origMap.put("TrxId", bindCardDTO.getTrxId());
		origMap.put("OriAuthTrxId", bindCardDTO.getOriAuthTrxId());
		origMap.put("SmsCode", bindCardDTO.getSmsCode());
		
		String respStr = null;
		try {
			//3.2 绑卡请求
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷确定绑卡请求参数:" + JSONObject.toJSONString(origMap));
			respStr = Post.gatewayPost(merchant.getRequestUrl(), origMap, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷确定绑卡返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>orderNumber" + bindCardDTO.getTrxId() + "确定绑卡请求异常:", e1);
		}
		Map<String, Object> result=new HashMap<String, Object>();
		if(StrUtil.isBlank(respStr)){
			throw new MarvelsBusinessException("返回结果为空");
	    }else{
	    	JSONObject resp = JSONObject.parseObject(respStr);
			if(StrUtil.equals(resp.getString("AcceptStatus"), "S") 
					&& StrUtil.equalsAny(resp.getString("Status"), "S")){
				result.put("errormsg","成功");
				result.put("status","BIND_SUCCESS");
		    }else{
		    	result.put("errormsg", resp.get("RetMsg"));
				result.put("status","BIND_FAIL");
		    }
			result.put("errorcode", resp.get("RetCode"));
	    }
		return result;
	}

	@Override
	public Map<String, Object> directPayment(DirectPaymentDTO directPaymentDTO) throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>畅捷协议代扣请求参数:"+JSON.toJSONString(directPaymentDTO));
		String loanId = directPaymentDTO.getLoanId();
		// 商户号
		MerchantDTO merchant = getMerchantNoDK();
		
		// 组装业务参数
		Map<String, String> origMap = new HashMap<String, String>();
		origMap = setCommonMap(origMap, merchant.getMerchantNo());
		origMap.put("Service", "nmg_biz_api_quick_payment");
		origMap.put("TrxId", directPaymentDTO.getTrxId());
		origMap.put("OrdrName", "畅捷支付");
		origMap.put("MerUserId", directPaymentDTO.getMerUserId());
		origMap.put("SellerId", merchant.getMerchantNo());
		origMap.put("SubMerchantNo", "");
		origMap.put("ExpiredTime", directPaymentDTO.getExpiredTime());
		origMap.put("CardBegin", StrUtil.subPre(directPaymentDTO.getCardNo(), 6));// 卡号前6位
		origMap.put("CardEnd", StrUtil.subSufByLength(directPaymentDTO.getCardNo(), 4));// 卡号后4位
		origMap.put("TrxAmt", directPaymentDTO.getTrxAmt());
		origMap.put("TradeType", directPaymentDTO.getTradeType());
		origMap.put("SmsFlag", directPaymentDTO.getSmsFlag());
		
		Map<String, Object> result=new HashMap<String, Object>();
		String respStr = null;
		try {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷协议代扣请求参数:" + JSONObject.toJSONString(origMap));
			respStr = Post.gatewayPost(merchant.getRequestUrl(), origMap, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷协议代扣返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>loanId" + loanId + "畅捷协议代扣请求异常:", e1);
			result.put("status",MarvelsConstant.PROCESSING);
			result.put("errormsg","协议代扣请求异常");
			return result;
		}
		
		
		if(StrUtil.isBlank(respStr))
		{
			result.put("status",MarvelsConstant.PROCESSING);
			result.put("errormsg","返回结果为空");
	    }
	    else
	    {
	    	JSONObject resp = JSONObject.parseObject(respStr);
	    	if(StrUtil.equals(resp.getString("AcceptStatus"), "S")){
				if(StrUtil.equals(resp.getString("Status"), "S")){
					result.put("status",MarvelsConstant.SUCCESS);
					result.put("errormsg","交易成功");
				}else if(StrUtil.equals(resp.getString("Status"), "P")){
					result.put("status", MarvelsConstant.PROCESSING + getExtension(resp, "memo"));
					result.put("errormsg", resp.get("RetMsg"));
				}else{
					result.put("status", MarvelsConstant.FAILED);
					result.put("errormsg", resp.get("RetMsg") + getExtension(resp, "memo"));
			    }
				result.put("r5_TrxNo", resp.get("OrderTrxid"));//支付平台流水号
				result.put("errorcode", resp.get("RetCode"));
	    	}else{
	    		result.put("status", MarvelsConstant.FAILED);
				result.put("errormsg", resp.get("RetMsg")  + getExtension(resp, "memo"));
				result.put("errorcode", resp.get("RetCode"));
	    	}
	    }
		return result;
	}

	@Override
	public Map<String, Object> directPaymentQuery(DirectPaymentQueryDTO directPaymentQueryDTO)
			throws MarvelsBusinessException {
		MarvelsLogUtil.getInstance().info(">>>>>>>>>>畅捷协议代扣结果查询请求参数:" + JSON.toJSONString(directPaymentQueryDTO));
		// 商户号
		MerchantDTO merchant = getMerchantNoDK();
		
		// 组装业务参数
		Map<String, String> origMap = new HashMap<String, String>();
		origMap = setCommonMap(origMap, merchant.getMerchantNo());
		origMap.put("Service", "nmg_api_query_trade");
		origMap.put("TrxId", directPaymentQueryDTO.getTrxId());
		origMap.put("OrderTrxId", directPaymentQueryDTO.getOrderTrxId());
		origMap.put("TradeType", directPaymentQueryDTO.getTradeType());
		
		Map<String, Object> result=new HashMap<String, Object>();
		String respStr = null;
		try {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷协议支付查询请求参数:" + JSONObject.toJSONString(origMap));
			respStr = Post.gatewayPost(merchant.getRequestUrl(), origMap, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷协议支付查询请求返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>origTransId" + directPaymentQueryDTO.getOrderTrxId() + "协议代扣结果查询请求:", e1);
			result.put("status",MarvelsConstant.PROCESSING);
			result.put("errormsg","协议代扣结果查询请求异常");
			return result;
		}
		
		if(StrUtil.isBlank(respStr)){
			result.put("status",MarvelsConstant.PROCESSING);
			result.put("errormsg", "返回结果为空");
	    }
	    else
	    {
	    	JSONObject resp = JSONObject.parseObject(respStr);
	    	if(StrUtil.equals(resp.getString("AcceptStatus"), "S")){
				if(StrUtil.equals(resp.getString("Status"), "S")){
					result.put("status",MarvelsConstant.SUCCESS);
					result.put("errormsg","交易成功");
				}else if(StrUtil.equals(resp.getString("Status"), "P")){
					result.put("status", MarvelsConstant.PROCESSING);
					result.put("errormsg", resp.get("RetMsg") + getExtension(resp, "memo"));
				}else{
					result.put("status", MarvelsConstant.FAILED);
					result.put("errormsg", resp.get("RetMsg") + getExtension(resp, "memo"));
			    }
				result.put("r5_TrxNo", resp.get("OrderTrxid"));//支付平台流水号
				result.put("errorcode", resp.get("RetCode"));
	    	}else{
	    		result.put("status", MarvelsConstant.PROCESSING);
				result.put("errormsg", resp.get("RetMsg") + getExtension(resp, "memo"));
				result.put("errorcode", resp.get("RetCode"));
	    	}
	    }
		return result;
	}

	@Override
	public Map<String, Object> pay(McfeRepaymentTradeDetails tradeDetail) {
		// 商户号
		MerchantDTO merchant = getMerchantNoDF();
		Map<String, Object> resultMap = new HashMap<String,Object>();
		
		Map<String, String> map = this.requestBaseParameter(merchant.getMerchantNo());
		map.put("TransCode", "T10000");
		map.put("OutTradeNo", tradeDetail.getPayOrderNumber());
		map.put("BusinessType", "0"); // 业务类型：0对私 1对公
		map.put("BankCommonName", tradeDetail.getBankName()); // 通用银行名称
		map.put("AccountType", "00"); // 账户类型
		map.put("AcctNo", RSA.encrypt(tradeDetail.getBankNumber(), merchant.getPublicKey(), charset)); // 对手人账号(此处需要用真实的账号信息)
		map.put("AcctName", RSA.encrypt(tradeDetail.getCustName(), merchant.getPublicKey(), charset)); // 对手人账户名称
		map.put("Phone", RSA.encrypt(tradeDetail.getMobileNo(), merchant.getPublicKey(), charset));
		map.put("TransAmt", tradeDetail.getAmount());
		//map.put("CorpAcctNo", "1223332343");  //企业账号可空
		//map.put("BankCode", "CCB");//对公必填
		String respStr = null;
		try {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷代付请求参数:" + JSONObject.toJSONString(map));
			respStr = ChanPayUtil.sendPost(merchant.getRequestUrl(), map, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷代付请求返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>origTransId" + tradeDetail.getPayOrderNumber() + "代付请求异常:", e1);
			resultMap.put("result", "1");
			resultMap.put("errorCode",MarvelsConstant.PROCESSING);
			resultMap.put("errorMsg","支付异常,需要查询结果");
			return resultMap;
		}
		
		if(StrUtil.isBlank(respStr)){
			resultMap.put("result", "1");
			resultMap.put("errorCode",MarvelsConstant.PROCESSING);
			resultMap.put("errorMsg", "返回结果为空");
	    } else{
	    	JSONObject resp = JSONObject.parseObject(respStr);
	    	if(StrUtil.equals(resp.getString("AcceptStatus"), "S")){
	    		if(StrUtil.equals(resp.getString("PlatformRetCode"), "0000")){
	    			if(StrUtil.equals(resp.getString("OriginalRetCode"), "000000")){
	    				resultMap.put("errorCode",MarvelsConstant.SUCCESS);
	    				resultMap.put("errorMsg","代付交易成功");
	    			}else if(StrUtil.equalsAny(resp.getString("OriginalRetCode"), "000001", "000002", "000004"
	    					, "000010", "000050", "900001", "900002", "900003")){
	    				resultMap.put("result", "1");
	    				resultMap.put("errorCode",MarvelsConstant.PROCESSING);
	    				resultMap.put("errorMsg", resp.getString("AppRetMsg"));
	    			}else{
	    				resultMap.put("result", "-1");
	    				resultMap.put("errorCode", MarvelsConstant.FAILED);
		    			resultMap.put("errorMsg", resp.getString("OriginalErrorMessage"));
	    			}
	    		}else if(StrUtil.equals(resp.getString("PlatformRetCode"), "2000")){
	    			resultMap.put("result", "1");
	    			resultMap.put("errorCode",MarvelsConstant.PROCESSING);
	    			resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    		}else{
	    			resultMap.put("result", "-1");
	    			resultMap.put("errorCode", MarvelsConstant.FAILED);
	    			resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    		}
	    	}else{
	    		resultMap.put("result", "-1");
				resultMap.put("errorCode", MarvelsConstant.FAILED);
				resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    	}
	    }
		return resultMap;
	}

	@Override
	public Map<String, Object> payResultQuery(McfeRepaymentTradeDetails tradeDetail) {
		// 商户号
		MerchantDTO merchant = getMerchantNoDF();

		Map<String, Object> resultMap = new HashMap<String,Object>();
		
		Map<String, String> map = this.requestBaseParameter(merchant.getMerchantNo());
		map.put("TransCode", "C00000");
		map.put("OutTradeNo", GenerateReqNoUtils.transctionReq2("CJDFCX",26));
		map.put("OriOutTradeNo", tradeDetail.getPayOrderNumber());
		
		String respStr = null;
		try {
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷代付结果查询请求参数:" + JSONObject.toJSONString(map));
			respStr = ChanPayUtil.sendPost(merchant.getRequestUrl(), map, charset, merchant.getPrivateKey());
			MarvelsLogUtil.getInstance().info(">>>>>>>>>>>>>畅捷代付结果查询请求返回参数:" + respStr);
		} catch (Exception e1) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>origTransId" + tradeDetail.getPayOrderNumber() + "代付结果查询请求异常:", e1);
			// 异常，需要重查结果
			resultMap.put("result", "1");
			resultMap.put("errorCode",MarvelsConstant.FAILED);
			resultMap.put("errorMsg","支付异常,需要查询结果");
			return resultMap;
		}
		
		if(StrUtil.isBlank(respStr)){
			// 异常，需要重查结果
			resultMap.put("result", "1");
			resultMap.put("errorCode",MarvelsConstant.FAILED);
			resultMap.put("errorMsg", "返回结果为空");
	    } else{
	    	JSONObject resp = JSONObject.parseObject(respStr);
	    	if(StrUtil.equals(resp.getString("AcceptStatus"), "S")){
	    		if(StrUtil.equals(resp.getString("PlatformRetCode"), "0000")){
	    			if(StrUtil.equals(resp.getString("OriginalRetCode"), "000000")){
	    				// 放款成功
	    				resultMap.put("result", "0");
	    				resultMap.put("errorCode",MarvelsConstant.SUCCESS);
	    				resultMap.put("errorMsg","放款成功");
	    			}else if(StrUtil.equalsAny(resp.getString("OriginalRetCode"), "000001", "000002", "000004"
	    					, "000010", "000050", "900001", "900002", "900003")){
	    				// 需要重查结果
	    				resultMap.put("result", "1");
	    				resultMap.put("errorCode",MarvelsConstant.SUCCESS);
	    				resultMap.put("errorMsg", resp.getString("AppRetMsg"));
	    			}else{
	    				// 放款失败
	    				resultMap.put("result", "-1");
	    				resultMap.put("errorCode", MarvelsConstant.SUCCESS);
		    			resultMap.put("errorMsg", resp.getString("OriginalErrorMessage"));
	    			}
	    		}else if(StrUtil.equals(resp.getString("PlatformRetCode"), "2000")){
	    			// 需要重查结果
	    			resultMap.put("result", "1");
	    			resultMap.put("errorCode",MarvelsConstant.SUCCESS);
	    			resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    		}else{
	    			// 放款失败
	    			resultMap.put("result", "-1");
	    			resultMap.put("errorCode", MarvelsConstant.SUCCESS);
	    			resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    		}
	    	}else{
	    		resultMap.put("result", "1");
				resultMap.put("errorCode", MarvelsConstant.FAILED);
				resultMap.put("errorMsg", resp.getString("PlatformErrorMessage"));
	    	}
	    }
		return resultMap;
	}

	/**
	 * 公共请求参数设置
	 */
	public Map<String, String> setCommonMap(Map<String, String> origMap, String partnerId) {
		// 2.1 基本参数
		origMap.put("Version", "1.0");
		origMap.put("PartnerId", partnerId);//生产环境测试商户号
		origMap.put("InputCharset", CharsetUtil.UTF_8);// 字符集
		origMap.put("TradeDate", DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN));// 商户请求日期
		origMap.put("TradeTime", DateUtil.format(DateUtil.date(), DatePattern.PURE_TIME_PATTERN));// 商户请求时间
		origMap.put("Memo", null);
		return origMap;
	}
	
	/**
	 * 代付公共参数
	 * @param service
	 * @param partnerId
	 * @return
	 */
	public Map<String,String> requestBaseParameter(String partnerId){
		Map<String, String> origMap = new HashMap<String, String>();
		// 2.1 基本参数
		origMap.put("Service", "cjt_dsf");
		origMap.put("Version", "1.0");
		origMap.put("PartnerId", partnerId); // 畅捷支付分配的商户号
		origMap.put("TradeDate", DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN));
		origMap.put("TradeTime", DateUtil.format(DateUtil.date(), DatePattern.PURE_TIME_PATTERN));
		origMap.put("InputCharset", CharsetUtil.UTF_8);// 字符集
		origMap.put("Memo", "");// 备注
		return origMap;
	}
	
	/**
	 * 获取畅捷商户号(协议代扣)
	 * @return
	 */
	private MerchantDTO getMerchantNoDK(){
		MerchantDTO merchantDTO = new MerchantDTO();
		McsmRevealConfigureAppDTO config = mcsmRevealConfigureAppDao.queryConfigByKey("cj.dk.merchant.no");
		if(config != null && config.getConfigValue() != null && config.getState() == 0){
			merchantDTO.setMerchantNo(config.getConfigValue());
		}
		// 请求地址
		String requestUrl = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.withhold.url");
		merchantDTO.setRequestUrl(requestUrl);
		// 商户私钥
		String privateKey = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.dk.merchant.private.key");
		merchantDTO.setPrivateKey(privateKey);
		// 畅捷公钥
		String publicKey = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.dk.merchant.public.key");
		merchantDTO.setPublicKey(publicKey);
		MarvelsLogUtil.getInstance().info("实时获取畅捷商户号(协议代扣)：" + JSONObject.toJSONString(merchantDTO));
		return merchantDTO;
	}
	
	/**
	 * 获取畅捷商户号(代付)
	 * @return
	 */
	private MerchantDTO getMerchantNoDF(){
		MerchantDTO merchantDTO = new MerchantDTO();
		McsmRevealConfigureAppDTO config = mcsmRevealConfigureAppDao.queryConfigByKey("cj.df.merchant.no");
		if(config != null && config.getConfigValue() != null && config.getState() == 0){
			merchantDTO.setMerchantNo(config.getConfigValue());
		}
		
		// 畅捷代付公钥
		String publicKey = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.df.merchant.public.key");
		merchantDTO.setPublicKey(publicKey);
		// 商户代付私钥
		String privateKey = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.df.merchant.private.key");
		merchantDTO.setPrivateKey(privateKey);
		// 请求代付地址
		String url = ServerProperties.getValue(configPropertiesPath, "common.properties", "cj.pay.url");
		merchantDTO.setRequestUrl(url);
		MarvelsLogUtil.getInstance().info("实时获取畅捷商户号(代付)：" + JSONObject.toJSONString(merchantDTO));
		return merchantDTO;
	}

	/**
	 * 获取扩展字段中的属性
	 * @param data
	 * @param key
	 * @return
	 * @author:wujunfeng
	 */
	private String getExtension(JSONObject data, String key){
		String result = "";
		try {
			JSONArray extensionList = data.getJSONArray("Extension");
			if(extensionList != null && extensionList.size() > 0){
				for (int i = 0; i < extensionList.size(); i++) {
					JSONObject extension = extensionList.getJSONObject(i);
					if(extension.containsKey(key)){
						result = extension.getString(key);
						break;
					}
				}
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("data:" + data.toString() + "，获取：" + key + "失败！");
		}
		result = StrUtil.nullToDefault(result, "");
		if(StrUtil.isNotBlank(result)){
			result = "[" + StrUtil.nullToDefault(result, "") + "]";
		}
		return result;
	}
}
